package com.greate.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.greate.community.dao.SectionMapper;
import com.greate.community.dao.UserMapper;
import com.greate.community.entity.Section;
import com.greate.community.entity.User;
import com.greate.community.service.SectionService;
import com.greate.community.service.UserService;
import com.greate.community.util.CommunityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SectionServiceImpl extends ServiceImpl<SectionMapper, Section> implements SectionService {

     @Autowired
    private SectionMapper sectionMapper;

     @Autowired
     private UserService userService;

     @Autowired
     private UserMapper userMapper;


    @Override
    public List<Section> getAllActiveSections() {
        LambdaQueryWrapper<Section> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Section::getStatus, 0);
        return sectionMapper.selectList(wrapper);
    }

    @Override
    public List<Section> getSectionsByUserId(Integer userId) {
        LambdaQueryWrapper<Section> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Section::getUserId, userId);
        return sectionMapper.selectList(queryWrapper);
    }

    @Override
    public Section getSectionsById(Long sectionId) {
        return sectionMapper.selectById(sectionId);
    }

    @Override
    public void updateStatus(int sectionId, int status) {
        Section section = sectionMapper.selectById(sectionId);
        section.setStatus(status);
        sectionMapper.updateById(section);
    }

    @Override
    public List<Section> getSectionsByParams(String sectionName, String username,String status,
                                             String startTime, String endTime) {
        Integer userId = null;
        if (!StringUtils.isEmpty(username)){
            User user = userService.getUserByName(username);
            if (user != null){
                userId = user.getId();
            } else userId = 1;
        }

        LambdaQueryWrapper<Section> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(sectionName), Section::getName, sectionName)
                .eq(!StringUtils.isEmpty(userId), Section::getUserId, userId)
                .eq(Integer.parseInt(status) != 999, Section::getStatus, Integer.parseInt(status));
        try {
            if (!StringUtils.isEmpty(startTime)){
                Date timeForm = null;
                timeForm = CommunityUtil.parseDate(startTime, CommunityUtil.DATE_TIME_FORMAT);
                wrapper.ge(!StringUtils.isEmpty(startTime), Section::getCreateTime,timeForm);
            }
            if (!StringUtils.isEmpty(endTime)){
                Date timeTo = CommunityUtil.parseDate(endTime, CommunityUtil.DATE_TIME_FORMAT);
                wrapper.le(!StringUtils.isEmpty(endTime), Section::getCreateTime, timeTo);
            }
        }catch (ParseException e) {
            throw new RuntimeException(e);
        }
        List<Section> sections = sectionMapper.selectList(wrapper);
        List<Section> result = new ArrayList<>();
        for (Section section : sections){
            User user = userService.getUserById(section.getUserId());
            section.setUser(user);
            result.add(section);
        }
        return result;
    }

    @Override
    public List<Section> getAllSectionsWithUser() {
        return sectionMapper.selectSectionsWithUser();
    }

    @Override
    public void changeModerator(String sectionId, String userId) {
        Section section = sectionMapper.selectById(sectionId);
        section.setUserId(Integer.parseInt(userId));
        LambdaQueryWrapper<Section> sectionWrapper = new LambdaQueryWrapper<>();
        sectionWrapper.eq(Section::getId, sectionId);
        sectionMapper.update(section, sectionWrapper);
        User user = userMapper.selectById(Integer.parseInt(userId));
        user.setType(2);
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getId, userId);
        userMapper.update(user, userWrapper);
    }

    @Override
    public void addSection(String name) {
        Section section = new Section();
        section.setName(name);
        section.setStatus(0);
        section.setCreateTime(new Date());
        sectionMapper.insert(section);
    }

}
